package com.cennavi.minenavidemo.widget;

import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.Color;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.view.animation.AlphaAnimation;
import android.view.animation.Animation;
import android.view.animation.AnimationSet;
import android.view.animation.ScaleAnimation;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.cennavi.minenavidemo.R;


/**
 * 导航界面中显示车速的view
 */
public class CarSpeedView extends RelativeLayout {
    public static final int SPEED_ANIMATION_TIME = 500; // 超速心跳动画时间
    public static final int RIPPLE_ANIMATION_TIME = SPEED_ANIMATION_TIME / 3; // 超速波纹动画时间
    public static final float RIPPLE_ANIMATION_TARGET_ALPHA = 0.2f; // 超速波纹透明度目标值

    private Context mContext;
    // 超速放大缩小动画
    private ScaleAnimation mSpeedZoomInAnimation, mSpeedZoomOutAnimation;
    // 超速波纹动画
    private AnimationSet mRippleAnimationSet1, mRippleAnimationSet2;
    private boolean mIsInAnimation;

    private LinearLayout mSpeedRootView;
    private TextView mSpeedTv;
    private TextView mSpeedUnitTv;
    private View mSpeedAnimView;
    private TextView mSpeedTextView;

    public CarSpeedView(Context context) {
        super(context);
        init(context);
    }

    public CarSpeedView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(context);
    }

    public CarSpeedView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context);
    }

    @SuppressLint("CutPasteId")
    private void init(Context context) {
        mContext = context;
        LayoutInflater.from(mContext).inflate(R.layout.minenavi_car_speed_view, this);
        mSpeedTextView = findViewById(R.id.tv_speed);
        mSpeedRootView = findViewById(R.id.ll_speed);
        mSpeedTv = findViewById(R.id.tv_speed);
        mSpeedUnitTv = findViewById(R.id.tv_speed_unit);
        mSpeedAnimView = findViewById(R.id.speed_anim_view);
    }

    public void startSpeedAnimation() {
        if (mIsInAnimation) {
            return;
        }
        mIsInAnimation = true;
        mSpeedRootView.setBackgroundResource(R.drawable.minenavi_route_speed_limit);
        mSpeedAnimView.setVisibility(View.VISIBLE);
        mSpeedTv.setTextColor(Color.parseColor("#eb2e1e"));
        mSpeedUnitTv.setTextColor(Color.parseColor("#eb2e1e"));

        //<-----------------放大动画--------------------->
        if (mSpeedZoomInAnimation == null) {
            mSpeedZoomInAnimation = new ScaleAnimation(1.0f, 1.1f, 1.0f, 1.1f,
                    ScaleAnimation.RELATIVE_TO_SELF, 0.5f,
                    ScaleAnimation.RELATIVE_TO_SELF, 0.5f);
            mSpeedZoomInAnimation.setDuration(SPEED_ANIMATION_TIME);
            mSpeedZoomInAnimation.setFillAfter(true);
            mSpeedZoomInAnimation.setAnimationListener(new Animation.AnimationListener() {
                @Override
                public void onAnimationStart(Animation animation) {
                }

                @Override
                public void onAnimationEnd(Animation animation) {
                    //<-----------------缩小动画--------------------->
                    if (mSpeedZoomOutAnimation == null) {
                        mSpeedZoomOutAnimation = new ScaleAnimation(1.1f, 1.0f, 1.1f, 1.0f,
                                ScaleAnimation.RELATIVE_TO_SELF, 0.5f,
                                ScaleAnimation.RELATIVE_TO_SELF, 0.5f);
                        mSpeedZoomOutAnimation.setDuration(SPEED_ANIMATION_TIME);
                        mSpeedZoomOutAnimation.setFillAfter(true);
                        mSpeedZoomOutAnimation.setAnimationListener(new Animation.AnimationListener() {
                            @Override
                            public void onAnimationStart(Animation animation) {

                            }

                            @Override
                            public void onAnimationEnd(Animation animation) {
                                if (mIsInAnimation) {
                                    mSpeedRootView.startAnimation(mSpeedZoomInAnimation);
                                    mSpeedAnimView.startAnimation(mRippleAnimationSet1);
                                }
                            }

                            @Override
                            public void onAnimationRepeat(Animation animation) {

                            }
                        });
                    }
                    if (mIsInAnimation) {
                        mSpeedRootView.startAnimation(mSpeedZoomOutAnimation);
                    }
                }

                @Override
                public void onAnimationRepeat(Animation animation) {
                }
            });
        }

        //<-----------------水波第一段动画--------------------->
        if (mRippleAnimationSet1 == null) {
            mRippleAnimationSet1 = new AnimationSet(true);
            ScaleAnimation scaleAnimation = new ScaleAnimation(1.0f, 1.35f, 1.0f, 1.35f,
                    ScaleAnimation.RELATIVE_TO_SELF, 0.5f,
                    ScaleAnimation.RELATIVE_TO_SELF, 0.5f);
            scaleAnimation.setDuration(RIPPLE_ANIMATION_TIME);
            AlphaAnimation rippleAlphaAnimation = new AlphaAnimation(1.0f, RIPPLE_ANIMATION_TARGET_ALPHA);
            rippleAlphaAnimation.setDuration(RIPPLE_ANIMATION_TIME);
            mRippleAnimationSet1.addAnimation(scaleAnimation);
            mRippleAnimationSet1.addAnimation(rippleAlphaAnimation);
            mRippleAnimationSet1.setDuration(RIPPLE_ANIMATION_TIME);
            mRippleAnimationSet1.setFillAfter(false);
            mRippleAnimationSet1.setAnimationListener(new Animation.AnimationListener() {
                @Override
                public void onAnimationStart(Animation animation) {

                }

                @Override
                public void onAnimationEnd(Animation animation) {
                    //<-----------------水波第二段动画--------------------->
                    if (mRippleAnimationSet2 == null) {
                        mRippleAnimationSet2 = new AnimationSet(true);
                        ScaleAnimation scaleAnimation = new ScaleAnimation(1.0f, 1.5f, 1.0f, 1.5f,
                                ScaleAnimation.RELATIVE_TO_SELF, 0.5f,
                                ScaleAnimation.RELATIVE_TO_SELF, 0.5f);
                        AlphaAnimation rippleAlphaAnimation = new AlphaAnimation(1.0f, RIPPLE_ANIMATION_TARGET_ALPHA);
                        scaleAnimation.setDuration(RIPPLE_ANIMATION_TIME);
                        rippleAlphaAnimation.setDuration(RIPPLE_ANIMATION_TIME);
                        mRippleAnimationSet2.addAnimation(scaleAnimation);
                        mRippleAnimationSet2.addAnimation(rippleAlphaAnimation);
                        mRippleAnimationSet2.setFillAfter(false);
                        mRippleAnimationSet2.setDuration(RIPPLE_ANIMATION_TIME);
                    }
                    mSpeedAnimView.startAnimation(mRippleAnimationSet2);
                }

                @Override
                public void onAnimationRepeat(Animation animation) {

                }
            });
        }

        mSpeedRootView.startAnimation(mSpeedZoomInAnimation);
        mSpeedAnimView.startAnimation(mRippleAnimationSet1);
    }


    public void stopSpeedAnimation() {
        if (!mIsInAnimation) {
            return;
        }
        mIsInAnimation = false;
        mSpeedRootView.setBackgroundResource(R.drawable.minenavi_naving_view_speed_bg);
        mSpeedAnimView.setVisibility(View.GONE);
        mSpeedTv.setTextColor(Color.parseColor("#3c78ff"));
        mSpeedUnitTv.setTextColor(Color.parseColor("#3c78ff"));

        mSpeedRootView.clearAnimation();
        mSpeedAnimView.clearAnimation();
    }

    public void setSpeed(String s) {
        mSpeedTextView.setText(s);
    }
}
